

'''
-- 创建权限表
CREATE TABLE permissions (
    permission_id SERIAL PRIMARY KEY,
    permission_name VARCHAR(50) NOT NULL UNIQUE,
    permission_path VARCHAR(255) NOT NULL UNIQUE,
    description TEXT
);
'''

from . import logger

class PermissionsTable:

    def __init__(self, connection, cursor):

        self.connection = connection
        self.cursor = cursor

    # 创建权限
    def create_permission(self, permission_name, permission_path, description):
        try:
            self.cursor.execute("INSERT INTO permissions (permission_name, permission_path, description) VALUES (%s, %s, %s)", (permission_name, permission_path, description))
            logger.info(f"Permission {permission_name} created successfully.")
            return True
        except Exception as e:
            logger.error(f"Error creating permission {permission_name}: {e}")
            return False

    # 获取所有权限
    def get_all_permissions(self, limit=10, offset=0):
        try:
            self.cursor.execute("SELECT * FROM permissions ORDER BY permission_id LIMIT %s OFFSET %s", (limit, offset))
            permissions = self.cursor.fetchall()
            logger.info(f"Got all permissions successfully.")
            return permissions
        except Exception as e:
            logger.error(f"Error getting all permissions: {e}")
            return None
        
    # 获取权限总数
    def get_permissions_count(self):
        try:
            self.cursor.execute("SELECT COUNT(*) FROM permissions")
            count = self.cursor.fetchone()['count']
            logger.info(f"Got permissions count successfully.")
            return count
        except Exception as e:
            logger.error(f"Error getting permissions count: {e}")
            return None

    # 根据权限ID获取权限
    def get_permission_by_id(self, permission_id):
        try:
            self.cursor.execute("SELECT * FROM permissions WHERE permission_id = %s", (permission_id,))
            permission = self.cursor.fetchone()
            logger.info(f"Got permission {permission_id} successfully.")
            return permission
        except Exception as e:
            logger.error(f"Error getting permission {permission_id}: {e}")
            return None

    # 根据权限名称获取权限
    def get_permission_by_name(self, permission_name):
        try:
            self.cursor.execute("SELECT * FROM permissions WHERE permission_name = %s", (permission_name,))
            permission = self.cursor.fetchone()
            logger.info(f"Got permission {permission_name} successfully.")
            return permission
        except Exception as e:
            logger.error(f"Error getting permission {permission_name}: {e}")
            return None

    # 更新权限
    def update_permission(self, permission_id, permission_name, permission_path, description):
        try:
            self.cursor.execute("UPDATE permissions SET permission_name = %s, permission_path = %s, description = %s WHERE permission_id = %s", (permission_name, permission_path, description, permission_id))
            logger.info(f"Permission {permission_id} updated successfully.")
            return True, None
        except Exception as e:
            logger.error(f"Error updating permission {permission_id}: {e}")
            return False, str(e)

    # 删除权限
    def delete_permission(self, permission_id):
        try:
            self.cursor.execute("DELETE FROM permissions WHERE permission_id = %s", (permission_id,))
            logger.info(f"Permission {permission_id} deleted successfully.")
            return True, None
        except Exception as e:
            logger.error(f"Error deleting permission {permission_id}: {e}")
            return False, str(e)
        
    # 根据权限路径获取权限
    def get_permission_by_path(self, permission_path):
        try:
            self.cursor.execute("SELECT * FROM permissions WHERE permission_path = %s", (permission_path,))
            permission = self.cursor.fetchone()
            logger.info(f"Got permission {permission_path} successfully.")
            return permission
        except Exception as e:
            logger.error(f"Error getting permission {permission_path}: {e}")
            return None